/*
 * ZENworks Notification Icon
 * Copyright (C) Author: Gangadhar Nagesh Metla (Novell, Inc.)
 * nmetla74@gmail.com 
 * Version 1.0
 * 1/3/2009
 * Filename BalloonMessage.java
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.novell.zenworks.agent.zicon;

import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.TrayItem;

import com.novell.zenworks.agent.common.ICommonUIEnums.MB_ICON;
import com.novell.zenworks.agent.common.ICommonUIEnums.MB_RESPONSE;
import com.novell.zenworks.agent.common.ICommonUIEnums.MB_TYPE;
import com.novell.zenworks.agent.zbrowser.MessageBoxLauncher;
import com.novell.zenworks.zicon.common.LogUtil;

public class BalloonMessage implements Runnable
{
    private Shell shell;
    private TrayItem item;
    private String message;
    private String title;    
    private MB_ICON iconType;
    private int timeout = 10000; //10 seconds by default.
    private static Object _lock = new Object();
    private static ToolTip ttip = null;


    public BalloonMessage(Shell shell, TrayItem item, String message, String title, MB_ICON iconType, int timeout)
    {
        super();
        this.shell = shell;
        this.item = item;
        this.message = message;
        this.title = title;
        this.iconType = iconType;
        
        if(timeout > 0)
            this.timeout = timeout;
    }

    public void run()
    {
    	try
    	{
    		if(this.message != null && item.getVisible())
    		{
    			synchronized(_lock)
    			{
    				if(ttip != null && !ttip.isDisposed())
    				{
    					ttip.setVisible(false);
    					ttip.dispose();
    					ttip = null;
    				}

    				switch(this.iconType)
    				{
    				    case MBI_NONE:
    				    {
    					    ttip = new ToolTip(shell, SWT.BALLOON);
    					    break;
    				    }
    				    case MBI_INFO:
    				    {
    					    ttip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION); 
    					    break;
    				    }
    				    case MBI_QUESTION:
    				    {
    					    ttip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_QUESTION);
    					    break;
    				    }
    				    case MBI_WARN:
    				    {
    					    ttip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_WARNING);
    					    break;
    				    }
    				    case MBI_ERROR:
    				    {
    					    ttip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_ERROR);  
    					    break;
    				    }
    				    default:
    				    {
    					    ttip = new ToolTip(shell, SWT.BALLOON);
    				    }
    				}
    				
    				ttip.setText(this.title);
    				ttip.setMessage(this.message);
    				ttip.setAutoHide(false);
    				item.setToolTip(ttip);               
    				ttip.setVisible(true);

    				Timer t = new Timer();
    				BalloonMessageTimer bmt = new BalloonMessageTimer(ttip);
    				t.schedule(bmt, timeout);
    			}
    		}
    	} 
    	catch (Exception e)
    	{
    		try
    		{
    			LogUtil.LogException(e.getMessage(), e);

    			if(e.getMessage() != null)
    			{
    				MessageBoxLauncher.ShowMessageBoxHandler(e.getMessage(), ZTrayIcon.rm.getString("PropDlgTitle") , MB_TYPE.MBT_OK, MB_ICON.MBI_ERROR, 30, false, MB_RESPONSE.MBR_OK);
    			}
    			else
    			{
    				MessageBoxLauncher.ShowMessageBoxHandler(ZTrayIcon.rm.getString("RemotingError"), ZTrayIcon.rm.getString("PropDlgTitle") , MB_TYPE.MBT_OK, MB_ICON.MBI_ERROR, 30, false, MB_RESPONSE.MBR_OK);
    			}

    		} catch (Exception e1)
    		{
    			LogUtil.LogException(e.getMessage(), e1);
    		}
    	}        
    }

}

